/* 
    * 2018-11 WQ添加
    * index模块主要是用于管理选项卡Tab的添加、删除，以及选项卡和导航栏的联动效果。
 */
layui.define(["layer", "admin", "element","config"], function (exports) {
    var e = layui.jquery,
        d = layui.admin,
        h = layui.element,
        config = layui.config,
        k = config.cacheTab,
        f = {
            cacheTab: void 0 == k ? !0 : k,
            loadView: function (b) {
                var a = b.menuPath,
                    c = b.menuName;
                if (a) {
                    var g = !1;
                    e(".layui-layout-admin .layui-body .layui-tab .layui-tab-title>li").each(function () {
                        if (e(this).attr("lay-id") === a) return g = !0, !1
                    });
                    if (!g && (h.tabAdd("admin-pagetabs", {
                            id: a,
                            title: c ? c : "无标题",
                            content: '<iframe src="' + a + '" frameborder="0" class="admin-iframe"></iframe>'
                        }), f.cacheTab)) {
                        (c = d.getTempData("indexTabs")) || (c = []);
                        for (var k = !1, l = 0; l < c.length; l++) b.menuPath == c[l].menuPath && (k = !0);
                        k || (c.push(b), d.putTempData("indexTabs", c))
                    }
                    h.tabChange("admin-pagetabs", a);
                    750 >= document.body.clientWidth && d.flexible(!0)
                } else console.error("地址不可为空！")
            },
            openTab: function (b) {
                f.loadView({
                    menuPath: b.url,
                    menuName: b.title
                })
            },
            closeTab: function (b) {
                h.tabDelete("admin-pagetabs", b)
            },
            closeTabCache: function () {
                f.cacheTab = !1;
                d.putTempData("indexTabs", void 0)
            },
            clearTabCache: function () {
                d.putTempData("indexTabs", void 0)
            },
            loadSetting: function () {
                if (f.cacheTab) {
                    var b = d.getTempData("indexTabs");
                    if (b) {
                        for (var a = d.getTempData("tabPosition"), c = 0, g = 0; g < b.length; g++) f.loadView(b[g]), b[g].menuPath == a && (c = g);
                        setTimeout(function () {
                            f.loadView(b[c])
                        }, 500)
                    }
                }
                a = config.openFooter;
                void 0 != a && 0 == a && (e(".layui-layout-admin .layui-footer").css("display", "none"), e(".layui-layout-admin .layui-body").css("bottom", "0"));
                config.tabAutoRefresh && e('.layui-body .layui-tab[lay-filter="admin-pagetabs"]').attr("lay-autoRefresh", "true")
            }
        };
    h.on("nav(admin-side-nav)", function (b) {
        var a = e(b);
        if ((b = a.attr("lay-href")) && "javascript:;" != b) a = a.text(), f.loadView({
            menuPath: b,
            menuName: a
        });
        else if ("true" == e(".layui-side .layui-nav-tree").attr("lay-accordion") && a.parent().hasClass("layui-nav-item")) {
            if (a.parent().hasClass("layui-nav-itemed") || a.parent().hasClass("layui-this")) e(".layui-layout-admin .layui-side .layui-nav .layui-nav-item").removeClass("layui-nav-itemed"), a.parent().addClass("layui-nav-itemed")
        } else d.setNavHoverCss(a.parentsUntil(".layui-nav-item").parent().children().eq(0))
    });
    h.on("tab(admin-pagetabs)", function (b) {
        b = e(this).attr("lay-id");
        d.rollPage("auto");
        d.activeNav(b);
        var a = e(".layui-layout-admin .layui-body .layui-tab-content .layui-tab-item.layui-show .admin-iframe")[0];
        a && (a.style.height = "99%", a.scrollWidth, a.style.height = "100%");
        a.focus();
        f.cacheTab && d.putTempData("tabPosition", b);
        "true" == e('.layui-body .layui-tab[lay-filter="admin-pagetabs"]').attr("lay-autoRefresh") && setTimeout(function () {
            top.layui.admin.refresh()
        }, 300)
    });
    h.on("tabDelete(admin-pagetabs)", function (b) {
        if (f.cacheTab) {
            b = e(this).parent().attr("lay-id");
            for (var a = d.getTempData("indexTabs"), c = 0; c < a.length; c++) b == a[c].menuPath && a.splice(c, 1);
            d.putTempData("indexTabs", a)
        }
    });
    exports("index", f)
});
